# -*- coding:utf-8 -*-

import requests
from lxml import etree
from fake_useragent import UserAgent
from http import cookiejar
import json
import time
from datetime import datetime

requests.packages.urllib3.disable_warnings()  # 忽略HTTPS安全警告

'''
美团-App-美食
上海 美食
http://apimeishi.meituan.com/meishi/filter/v7/deal/select?offset=0&ci=10&mypos=41.120617709771366%2C122.96264564263679&metrics_start_time={start_time}
商家信息
http://apimeishi.meituan.com/meishi/poi/v2/poi/base/{poiid}?uuid=0000000000000A80359FC7F904ABE9B210AEBE93CA6B2A153437614502539080&utm_medium=android&version_name=10.0.801
评价信息
http://apimeishi.meituan.com/meishi/poi/v2/poi/comment/list/{poiid}
'''
class MeiTuan():
    def __init__(self):
        #声明一个CookieJar对象实例来保存cookie
        self.cookie = cookiejar.CookieJar()
        # ua = UserAgent(use_cache_server=False)  # 禁用服务器缓存
        self.headers = {
            # "User-Agent": ua.random,
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36'
        }

    def get_contents(self):
        # 搜索商品URL
        offset = 0  # 每次递增25
        cityId = 10  # 城市ID 上海10
        start_time = str(int(time.time()))
        url = f"http://apimeishi.meituan.com/meishi/filter/v7/deal/select?offset={offset}&ci=10&mypos=41.120617709771366%2C122.96264564263679&metrics_start_time={start_time}&cityId={cityId}"

        html = HttpUtils.do_request("GET", url, self.headers, "")

        data_json = json.loads(html.text)

        for d in data_json['data']['poiList']['poiInfos']:
            dict_data = dict()
            dict_data['avgPrice'] = d['avgPrice']   # 平均价格
            dict_data['avgScore'] = d['avgScore']   # 评分
            dict_data['cateName'] = d['cateName']   # 类别 火锅
            dict_data['poiid'] = d['poiid']   # 商家ID
            dict_data['lat'] = d['lat']   # 北纬
            dict_data['lng'] = d['lng']   # 东经
            dict_data['name'] = d['name']   # 名称
            dict_data['areaName'] = d['areaName']   # 区域
            dict_data['rotationTags'] = d.get('traceData').get('sale')   # 季度销量
            print(str(dict_data))
            # self.get_comments1(d['poiid'])


    # 获取商家具体信息
    def get_comments1(self, poiid):
        url = f"http://apimeishi.meituan.com/meishi/poi/v2/poi/base/{poiid}?uuid=0000000000000A80359FC7F904ABE9B210AEBE93CA6B2A153437614502539080&utm_medium=android&version_name=10.0.801"
        html = HttpUtils.do_request("GET", url, self.headers, "")
        data_json1 = json.loads(html.text)
        dict_data = dict()
        dict_data['phone'] = data_json1['data']['phone']   # 电话
        dict_data['avgScore'] = data_json1['data']['avgScore']   # 评分
        dict_data['markNumbers'] = data_json1['data']['markNumbers']   # 评论总数
        dict_data['avgPrice'] = data_json1['data']['avgPrice']   # 平均价格
        dict_data['addr'] = data_json1['data']['addr']   # 地址
        dict_data['hotBoard'] = data_json1['data']['hotBoard']['boardInfo']   # 热榜
        dict_data['todayBusinessHoursInfo'] = data_json1['data']['todayBusinessHoursInfo']['status']   # 营业状态
        dict_data['todayBusinessHoursInfo'] = data_json1['data']['todayBusinessHoursInfo']['detail']   # 营业时间


        print(str(dict_data))

    # 获取评论
    def get_comments2(self, poiid):
        url = f"http://apimeishi.meituan.com/meishi/poi/v2/poi/comment/list/{poiid}"
        html = HttpUtils.do_request("GET", url,self.headers, "")
        data_json = json.loads(html.text)
        for d in data_json["data"]:
            dict_data = dict()
            dict_data['commentCount'] = d['commentCount']   # 总评价数量
            for d in d['commentTags']:
                dict_data['label'] = d['label']   # 评价标签
                dict_data['count'] = d['count']   # 标签数量

            print(str(dict_data))

    # 推荐菜
    def get_contents3(self, poiid):
        url = f"http://apimeishi.meituan.com/meishi/poi/v1/poi/mergedish/dishbrieflist/{poiid}?uuid=0000000000000A80359FC7F904ABE9B210AEBE93CA6B2A153437614502539080&utm_medium=android&version_name=10.0.801"
        html = HttpUtils.do_request("GET", url, self.headers, "")
        data_json1 = json.loads(html.text)
        for d in data_json1['data']['recommendDishes']['dishList']:
            dict_data = dict()
            dict_data['name'] = d['name']  # 名称
            dict_data['price'] = d['price']  # 价格
            dict_data['recCount'] = d['recCount']  # 推荐人数
            print(str(dict_data))


if __name__ == '__main__':
    meituan = MeiTuan()
    meituan.get_contents()